$(function(){ 
	var verificationCode;
	var passwordStatus = 0;
	var userNameStatus = 0;
	var codeStatus = 0;
	var sendPhone = '';
	
	//发送验证码
	var countdown=3;
	$('#sendVerificationCode').click(function(){
		console.log('点击');
		/*if ($('#sendVerificationCode').val() != '免费获取验证码') {
			console.log('111');
			return;
		}*/
		console.log('phone:' + $('#phoneInput').val());
		$.ajax({
			url:'register/sendSms',
			type:'POST',
			async:true,
			data:{
				phone:$('#phoneInput').val()
			},
			timeout:5000,
			dataType:'json',
			beforeSend:function(xhr){
				console.log("xhr:" +xhr);
		        console.log('发送前');

			},
			success:function(data){
				console.log(data);
				codeStatus = 1;
				sendPhone = data;
			},
			error:function(data){
				console.log(data)

			},
			complete:function(){
				console.log('完成');
			}
		})
		settime();
	});
	
	//是否同意条款
	$('#agree').click(function(){
		console.log($('#agree').val());
		var agreeVal = $('#agree').val();
		if (agreeVal == '0') {
			$('#agree').val('1');
			$('#submit').attr('disabled',false);
		} else {
			$('#agree').val('0');
			$('#submit').attr('disabled',true);
		}
	})
	
	//用户名判断
	$('#userName').blur(function(){
		var userName = $('#userName').val();
		log('userName',userName);
		$.post('register/validateUserName',{userName:$('#userName').val()}).success(function(data) {
			log("data",data.status);
			if (data.status) {
				$('#hasExsit').css('display','inline');
				userNameStatus = 0;
			} else {
				$('#hasExsit').css('display','none');
				userNameStatus = 1;
			}
		});
		
		
	})
	
	//两次密码是否一致
	$('#repeatPassword').blur(function() {
		var repeatPassword = $('#repeatPassword').val();
		var password = $('#password').val();
//		log("repeatPassword",repeatPassword);
//		log("password",password);
		if (repeatPassword != password) {
//			console.log('不相等')
			$('#notSameSpan').css('display','inline');
			passwordStatus = 0;
		} else {
//			console.log('相等')
			$('#notSameSpan').css('display','none');
			passwordStatus = 1;
		}
		
	});
	
	$('#password').blur(function() {
		var repeatPassword = $('#repeatPassword').val();
		var password = $('#password').val();
		log('password', password);
		if (repeatPassword != '') {
			if (repeatPassword != password) {
				$('#notSameSpan').css('display','inline');
				passwordStatus = 0;
			} else {
				$('#notSameSpan').css('display','none');
				passwordStatus = 1;
			}
		}
		
	});
	
	//手机号码校验
	$('#phoneInput').bind('input propertychange', function() {
		if((/^1[34578]\d{9}$/.test($('#phoneInput').val()))){
			$('#sendVerificationCode').attr('disabled',false);
		} else {
			$('#sendVerificationCode').attr('disabled',true);
		}
	});
	
	function settime() {
		var sendButton = $('#sendVerificationCode');
		if (countdown == 0) {
			console.log('0');
			log('countdown',countdown);
			sendButton.css('background', '#A020F0');
			sendButton.attr("disabled", false);
	        sendButton.val('免费获取验证码');
	        countdown = 3;
	        return;
	    } else {
	    	console.log('countdown');
	    	log('countdown',countdown);
	    	sendButton.css('background', '#ccc');
	    	sendButton.attr("disabled", true);
	    	sendButton.val("重新发送(" + countdown + ")");
	        countdown--;
	    }
		setTimeout(function() {
			settime()},1000)
			
	}
	
	/*$('#test').click(function() {
		$.ajax({
			url:'register/test',
			type:'POST',
			data:{
				phone:$('#phoneInput').val(),
				verificationCode:$('#codeInput').val()
			},
			timeout:5000,
			dataType:'json',
			success:function(data){
				console.log('正确');
				//window.location.href = 'login';
			},
			error:function(data){
				console.log('失败');
				console.log(data)

			}
		});
	});*/
	
	//表单提交---ajax是可以在success中跳转页面的，但如果和form在一起，最好在下面进行表单提交跳转页面
	$('#submit').click(function() {
		console.log('发送请1求');
		var formData = $('#registerForm').serialize();
		log("formData", formData);
		//序列化获得表单数据，结果为：user_id=12&user_name=John&user_age=20
		//var submitData=decodeURIComponent(data,true);
		
		var phone = $('#phoneInput').val();
		var code = $('#codeInput').val();
		var userName = $('#userName').val();
		var password = $('#password').val();
		var repeatPassword = $('#repeatPassword').val();
		log("code", code);
		log("verificationCode", verificationCode);
		log("passwordStatus", passwordStatus);
		log("userNameStatus", userNameStatus);
		if (!(/^1[34578]\d{9}$/.test($('#phoneInput').val()))) {
			return;
		}
		if (passwordStatus == 0 || userNameStatus == 0) {
			return;
		}
		
		if (codeStatus == 1) {
			if (sendPhone != phone) {
				return;
			}
		}
		
		//判断验证码是否正确
		$.ajax({
			url:'register/queryLastVerificationCode',
			type:'POST',
			async:false,
			data:{
				phone:$('#phoneInput').val(),
				verificationCode:$('#codeInput').val()
			},
			timeout:5000,
			dataType:'json',
			success:function(data){
				console.log('成功');
				if (!data) {
					alert('验证码错误');
					$('vericationCodeSpan').css('display', 'inline');
					return;
				} else {
					console.log('正确');
					$('vericationCodeSpan').css('display', 'none');
				}
			},
			error:function(data){
				console.log('失败');
				console.log(data)

			}
		});
		
		
		/*$.post('register/queryLastVerificationCode',{phone:$('#phoneInput').val(),verificationCode:$('#codeInput').val()}).success(function(data) {
			if (!data) {
				alert('验证码错误');
				$('vericationCodeSpan').css('display', 'inline');
				return;
			} else {
				$('vericationCodeSpan').css('display', 'none');
			}
		});*/
		
		$.ajax({
			url:'register/addUser',
			type:'POST',
			async:false,
			data:{
				phone:$('#phoneInput').val(),
				userName:$('#userName').val(),
				password:$('#password').val()
			},
			timeout:5000,
			dataType:'json',
			beforeSend:function(xhr){
				console.log("xhr:" +xhr);
		        console.log('发送前');

			},
			success:function(data){
				if (data.status) {
					console.log("添加成功");
					$('#registerForm').submit();
				} else {
					console.log("添加失败");
					alert('注册失败！');
					return false;
				}
				
			},
			error:function(data){
				console.log("失败");
				console.log(data);

			}
		});
		
		/*$.post('register/addUser',formData).success(function() {
			console.log('注册成功');
		});*/
	
		
	})
	
});